var bSaveOptionsDebugOutput = false;

function saveBooleanOption(bValue, sLocalStorageName) {
  localStorage[sLocalStorageName] = bValue ? 'true' : 'false';
  if (bSaveOptionsDebugOutput) console.log('  Saving boolean option ' + repr(sLocalStorageName) + ' = ' + repr(bValue));
}

function saveIntegerOption(iValue, sLocalStorageName) {
  localStorage[sLocalStorageName] = iValue.toString();
  if (bSaveOptionsDebugOutput) console.log('  Saving integer option ' + repr(sLocalStorageName) + ' = ' + repr(iValue));
}

function saveKeyDescriptionOption(sValue, sLocalStorageName) {
  localStorage[sLocalStorageName] = sValue;
  if (bSaveOptionsDebugOutput) console.log('  Saving key description option ' + repr(sLocalStorageName) + ' = ' + repr(sValue));
}

function saveNumberOption(nValue, sLocalStorageName) {
  localStorage[sLocalStorageName] = nValue.toString();
  if (bSaveOptionsDebugOutput) console.log('  Saving number option ' + repr(sLocalStorageName) + ' = ' + repr(nValue));
}

function saveOptions(oStructure, oValues) {
  saveOptionsGroup(oStructure, oValues, '');
  if (bSaveOptionsDebugOutput) console.log('Options = ' + repr(oValues));
}

function saveOptionsGroup(oStructure, oValues, sGroupName) {
  if (bSaveOptionsDebugOutput)
      if (sGroupName) {
        console.log('  Saving ' + repr(sGroupName) + ' options...');
      } else {
        console.log('Saving options...');
      }
  for (var sMemberName in oStructure) {
    var oMemberStructure = oStructure[sMemberName];
    var oValue = oValues[sMemberName];
    var sLocalStorageName = (sGroupName ? sGroupName + '.' : '') + sMemberName;
    switch (oMemberStructure.type) {
      case 'boolean':
        saveBooleanOption(oValue, sLocalStorageName);
        break;
      case 'group':
        saveOptionsGroup(oMemberStructure.members, oValue, sLocalStorageName);
        break;
      case 'integer':
        saveIntegerOption(oValue, sLocalStorageName);
        break;
      case 'key description':
        saveKeyDescriptionOption(oValue, sLocalStorageName);
        break;
      case 'list':
        saveOptionsList(oMemberStructure.items, oValue, sLocalStorageName);
        break;
      case 'number':
        saveNumberOption(oValue, sLocalStorageName);
        break;
      case 'text':
        saveTextOption(oValue, sLocalStorageName);
        break;
      default:
        assertionError('Unknown option type ' + repr(oMemberStructure.type) + '.');
    }
  }
}

function saveOptionsList(oStructure, aValues, sLocalStorageName) {
  localStorage[sLocalStorageName + '.length'] = aValues.length;
  if (bSaveOptionsDebugOutput) console.log('  Saving menu list ' + repr(sLocalStorageName) + ' (' + aValues.length + ' entries)');
  for (var i = 0; i < aValues.length; i++) {
    saveOptionsGroup(oStructure, aValues[i], sLocalStorageName + '[' + i + ']');
  }
}

function saveTextOption(sValue, sLocalStorageName) {
  localStorage[sLocalStorageName] = sValue;
  if (bSaveOptionsDebugOutput) console.log('  Saving text option ' + repr(sLocalStorageName) + ' = ' + repr(sValue));
}
